﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KnowledgeCommunity.Web.Models;
using KnowledgeCommunity.Model;
using KnowledgeCommunity.Service;

namespace KnowledgeCommunity.Web.DbHelper
{
    public class CommentHelper
    {
        CKCDB mydb = new CKCDB();
        /// <summary>
        /// FindAll()
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Comment> FindAll()
        {
            var apps = from n in mydb.Comments
                       orderby n.AddTime descending
                       select n;
            return apps;
        }
        /// <summary>
        /// FindById(int id)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Comment FindById(int id)
        {
            var app = mydb.Comments.Find(id);
            return app;
        }

        /// <summary>
        /// FindByShapeCode()
        /// </summary>
        /// <param name="code">编码</param>
        /// <param name="type">评论所属实体编号：A-文档；B-知识；C-商品</param>
        /// <returns></returns>
        public IEnumerable<Comment> FindByShapeCodeAndType(string code,string type) 
        {
            var apps = from n in mydb.Comments
                       where n.ShapeCode == code && n.BelongTo==type
                       orderby n.AddTime descending
                       select n;
            return apps;
        }
        /// <summary>
        /// FindByUserName()
        /// </summary>
        /// <param name="name">用户名</param>
        /// <returns></returns>
        public IEnumerable<Comment> FindByUserName(string name)
        {
            var apps = from n in mydb.Comments
                       where n.UserName == name
                       orderby n.AddTime descending
                       select n;
            return apps;
        }

        /// <summary>
        /// return 0-Fail; 1-Success
        /// </summary>
        /// <param name="model">Comment</param>
        /// <returns></returns>
        public bool Create(Comment model)
        {
            try
            {
                mydb.Comments.Add(model);
                mydb.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                string message = "Create in table<Comments>" + ex.ToString();
                WriteLog.WriteSysLog(message);
                return false;
            }
        }

        /// <summary>
        /// return 0-Fail; 1-Success
        /// </summary>
        /// <param name="model">Comment</param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            var model = FindById(id);
            try
            {
                mydb.Comments.Remove(model);
                mydb.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                string message = "Delete in table<Comments>" + ex.ToString();
                WriteLog.WriteSysLog(message);
                return false;
            }
        }
    }
}